This resembles the siamese network https://keras.io/getting-started/functional-api-guide/#shared-layers
In [1]:
import keras
from keras.layers import Input, LSTM, Dense
from keras.models import Model
In [2]:
tweet_a = Input(shape=(140, 256))
tweet_b = Input(shape=(140, 256))
To share a layer across different inputs, simply instantiate the layer once, then call it on as many inputs as you want:
In [3]:
# This layer can take as input a matrix
# and will return a vector of size 64
shared_lstm = LSTM(64)
In [4]:
# When we reuse the same layer instance
# multiple times, the weights of the layer
# are also being reused
# (it is effectively *the same* layer)
encoded_a = shared_lstm(tweet_a)
encoded_b = shared_lstm(tweet_b)
In [5]:
# We can then concatenate the two vectors:
merged_vector = keras.layers.concatenate([encoded_a, encoded_b], axis=-1)
# And add a logistic regression on top
predictions = Dense(1, activation='sigmoid')(merged_vector)
# We define a trainable model linking the
# tweet inputs to the predictions
model = Model(inputs=[tweet_a, tweet_b], outputs=predictions)
model.compile(optimizer='rmsprop',
loss='binary_crossentropy',
metrics=['accuracy'])
model.fit([data_a, data_b], labels, epochs=10)
Example architecture needs to find data to run example
In [ ]: